using Domain.Entities;
using Domain.Services;
using System;
using UserInterface.Misc;

namespace UserInterface.Admin
{
    public partial class EditGroup : System.Web.UI.Page
    {
        private short groupId;

        protected void Page_Load(object sender, EventArgs e)
        {
            groupId = Convert.ToInt16(Request.QueryString["groupId"]);

            if (!IsPostBack)
            {
                Group group = GroupService.GetById(groupId);

                GroupNameLiteral.Text = group.Name;

                GroupNameTextBox.Text = group.Name;
                PrefixTextBox.Text = group.Prefix;
                SuffixTextBox.Text = group.Suffix;
                CanViewActiveUserlistRadioButtonList.SelectedValue = group.CanViewActiveUserlist.ToString();
                CanViewUserlistRadioButtonList.SelectedValue = group.CanViewUserlist.ToString();
                CanViewProfilesRadioButtonList.SelectedValue = group.CanViewProfiles.ToString();
                CanUseSearchRadioButtonList.SelectedValue = group.CanUseSearch.ToString();
                IsAdministratorGroupRadioButtonList.SelectedValue = group.IsAdministratorGroup.ToString();
            }
        }

        protected void EditGroupButton_Click(object sender, EventArgs e)
        {
            Group group = GroupService.GetById(groupId);

            string name = GroupNameTextBox.Text;
            string prefix = PrefixTextBox.Text;
            string suffix = SuffixTextBox.Text;
            bool canViewActiveUserlist = Convert.ToBoolean(CanViewActiveUserlistRadioButtonList.SelectedValue);
            bool canViewUserlist = Convert.ToBoolean(CanViewUserlistRadioButtonList.SelectedValue);
            bool canViewProfiles = Convert.ToBoolean(CanViewProfilesRadioButtonList.SelectedValue);
            bool canUseSearch = Convert.ToBoolean(CanUseSearchRadioButtonList.SelectedValue);
            bool isAdministratorGroup = Convert.ToBoolean(IsAdministratorGroupRadioButtonList.SelectedValue);

            GroupService.UpdateGroup
            (
                group,
                name,
                prefix,
                suffix,
                canViewActiveUserlist,
                canViewUserlist,
                canViewProfiles,
                canUseSearch,
                isAdministratorGroup
            );
        }
    }
}
